package dao

import (
	"go-demo/model"
	"time"
)

// 添加新的评论到数据库
func CreateComment(userid int64, videoId int64, text string) {
	comment := model.Comment{UserId: userid, VideoId: videoId, Content: text, CreateDate: time.Now()}
	Db.Create(&comment)
}

// 用用户id和视频id查询返回评论
func GetCommentByUserIDAndVideoId(userid int64, videoId int64) (model.Comment, error) {
	var comment model.Comment
	result := Db.First(&comment, userid, videoId)
	if result.Error != nil {
		return comment, result.Error
	}
	return comment, nil
}

// 通过ID查询评论
func GetCommentByID(id int64) (model.Comment, error) {
	var comment model.Comment
	result := Db.First(&comment, id)
	if result.Error != nil {
		return comment, result.Error
	}
	return comment, nil
}

// 通过视频id查评论列表
func GetCommentByVideoID(videoid int64) ([]model.Comment, error) {
	// 通过用户id在点赞表中查询视频列表
	var comment []model.Comment
	result := Db.Where("video_id = ?", videoid).Find(&comment)
	if result.Error != nil {
		return nil, result.Error
	}
	return comment, nil
}

// 通过评论ID删除评论
func DeleteCommentByID(id int64) {
	Db.Delete(&model.Comment{}, id)
}
